import 'package:flutter/material.dart';
import 'package:flutter_app/bean/list_self_sign_collect_entity.dart';
import 'package:flutter_app/ui/apply/duty_cancel_dialog/duty_cancel_dialog_view.dart';
import 'package:flutter_app/ui/apply/duty_dialog/duty_dialog_view.dart';
import 'package:flutter_app/utils/toast_utils.dart';
import 'package:flutter_app/values/MyColors.dart';
import 'package:flutter_app/widget/custom_app_bar.dart';
import 'package:flutter_app/widget/empty_view.dart';
import 'package:flutter_screenutil/src/size_extension.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../../r.dart';
import 'my_apply_logic.dart';

/**
 * @Description: 我的报名
 * @Author: 下雨了
 * @CreateDate: 2021-11-1 10:56
 * @UpdateUser: 更新者
 * @UpdateDate:  2022-6-28 11:42:30
 * @UpdateRemark: getx
 * @Version: 1.1
 */
///
class MyApplyPage extends StatefulWidget {
  final String title;

  const MyApplyPage({Key? key, required this.title}) : super(key: key);

  @override
  _MyApplyPageState createState() => _MyApplyPageState();
}

class _MyApplyPageState extends State<MyApplyPage> {
  final logic = Get.put(MyApplyLogic());
  final state = Get.find<MyApplyLogic>().state;

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MyApplyLogic>(builder: (logic) {
      return Scaffold(
        backgroundColor: MyColors.FFF5F5F5,
        appBar: MyAppBar(context, title: "我的报名"),
        body: SmartRefresher(
          controller: logic.refreshController,
          //  允许下拉刷新
          enablePullDown: true,
          // 允许上拉加载
          enablePullUp: true,
          // 刷新
          onRefresh: logic.onRefresh,
          // 加载
          onLoading: logic.onLoading,
          header: WaterDropHeader(complete: Text('刷新完成', style: TextStyle(color: MyColors.FF333333))),
          footer: ClassicFooter(loadingText: '加载中...', idleText: '上拉加载更多..', noDataText: '没有更多数据了'),
          child: Container(
            margin: EdgeInsets.only(left: 16.r, right: 16.r),
            child: (state.listSelfSignCollectEntity?.content?.length ?? 0) > 0 ? ListView.builder(
              padding: EdgeInsets.only(top: 0),
              itemCount: state.listSelfSignCollectEntity!.content!.length,
              physics: BouncingScrollPhysics(),
              itemBuilder: (BuildContext context, int index) {
                return ListItem(state.listSelfSignCollectEntity!.content![index]);
              },
            ) : EmptyView(),
          ),
        ),
      );
    });
  }

  // 我的报名 item
  // taskType 1明查暗访  2 窗口值班
  // collectStatus	征集表状态 1征集结果中2分组中3已发布
  Widget ListItem(ListSelfSignCollectContent item) {
    return Container(
      margin: EdgeInsets.only(top: 12.r),
      decoration: BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(2.67.r)),
          color: Colors.white),
      child: Stack(
        children: [
          Visibility(visible: item.collectStatus! != "1", child: Align(alignment: Alignment.topRight, child: Image.asset(R.assetsImgIcon53, height: 47.5.r))),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                margin: EdgeInsets.only(left:16.r, top: 16.r),
                child: Text(item.taskName!, style: TextStyle(color:  MyColors.FF333333, fontSize: 15.sp, fontWeight: FontWeight.w600),),
              ),
              Container(
                margin: EdgeInsets.only(left:16.r, top: 6.r),
                child: Row(
                  children: [
                    // Image.asset(item.taskType == "1" ? R.assetsImgIcon5 : R.assetsImgIcon36, height: 18,),
                    Container(
                      padding: EdgeInsets.fromLTRB(6.r, 1.r, 6.r, 1.5.r),
                      decoration: BoxDecoration(
                          color: MyColors.FFFDE6E5,
                          borderRadius: BorderRadius.circular(2.r)
                      ),
                      child: Text(item.taskType == "1" ? "暗访" : item.taskType == "2" ? "值班" : "其他", style: TextStyle(color: MyColors.FFF03F35, fontSize: 12.sp),),
                    ),
                    Visibility(
                      visible: int.parse(item.limitDay!) > 0,
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Container(
                            padding: EdgeInsets.fromLTRB(6.r, 1.r, 6.r, 1.5.r),
                            margin: EdgeInsets.only(left: 5.r),
                            decoration: BoxDecoration(
                                color: MyColors.FFECF6FE,
                                borderRadius: BorderRadius.all(Radius.circular(2.r))
                            ),
                            child: Row(
                              children: [
                                Container(margin: EdgeInsets.only(top: 1.r, right: 3.5.r), child: Image.asset(R.assetsImgIcon54, height: 10.5.r,)),
                                Text(item.limitDay! + "天后结束", style: TextStyle(color: MyColors.FF2763B9, fontSize: 12.sp),),
                              ],
                            ),
                          )
                        ],
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.only(left: 5.r),
                      padding: EdgeInsets.fromLTRB(6.r, 1.r, 6.r, 1.5.r),
                      decoration: BoxDecoration(
                          color: MyColors.FFECF6FE,
                          borderRadius: BorderRadius.all(Radius.circular(2.r))
                      ),
                      child: Text("共报名"+ item.totalSign! +"人", style: TextStyle(color: MyColors.FF2763B9, fontSize: 12.sp),),
                    )
                  ],
                ),
              ),
              Container(
                margin: EdgeInsets.only(top: 12.r),
                height: 0.5.r,
                color: MyColors.FFDDDDDD,
              ),
              Container(
                margin: EdgeInsets.fromLTRB(16.r, 8.r, 16.r, 9.5.r),
                child: Row(
                  children: [
                    Expanded(child: Text(item.taskType == "2" ? "可以申请取消已安排的值班" : "可以查看任务详情", style: TextStyle(color: MyColors.FF777777, fontSize: 12.sp,),)),
                    Container(
                      height: 26.r,
                      child: OutlinedButton(
                        style: ButtonStyle(
                            padding: MaterialStateProperty.all(EdgeInsets.only(left: 10.r, right: 10.r)),
                            side: MaterialStateProperty.all(BorderSide(
                              width: 0.5.r,
                              color: MyColors.FF2763B9,
                            ),)
                        ),
                        child: Text(item.taskType! == "2" ? "管理值班日期" : "查看详情", style: TextStyle(color: MyColors.FF2763B9, fontSize: 13.sp),),
                        onPressed: () {
                          if (item.taskType == "") {
                            return toast.show("任务已过期");
                          }
                          if (item.taskType == "1" || item.taskType == "3") {
                            logic.goToApplyDetailPage(item.collectId);
                          } else {
                            showModalBottomSheet(
                              context: context,
                              shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.only(topLeft: Radius.circular(13.33.r), topRight: Radius.circular(13.33.r))
                              ),
                              isScrollControlled: true,
                              builder: (context) {
                                if (item.collectStatus == "3") {
                                  return DutyCancelDialogPage(
                                    title: "管理值班日期",
                                    collectId: item.collectId!,
                                  );
                                } else {
                                  return DutyDialogPage(
                                    title: "值班弹窗",
                                    collectId: item.collectId!,
                                    status: item.status!,
                                    onRefreshCallback: (String collectId) {
                                      logic.onRefresh();
                                    },
                                  );
                                }
                              },
                            );
                          }
                        },
                      ),
                    )
                  ],
                ),
              )
            ],
          ),
        ],
      ),
    );
  }

  @override
  void dispose() {
    Get.delete<MyApplyLogic>();
    super.dispose();
  }
}
